﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Table=FCNS.Data.Table;
using System.Diagnostics;
using System.Data;
using DoubleH.Utility;

namespace PayS
{
    /// <summary>
    /// ShouKuan.xaml 的交互逻辑
    /// </summary>
    public partial class OtherBill : Window
    {
        public OtherBill()
        {
            InitializeComponent();

            InitEvent();
        }

        Table.PayS order = null;
        public void Init(DataTableText tableText, Table.PayS obj)
        {
            order = obj ?? new Table.PayS(tableText == DataTableText.其它费用收入单 ? Table.PayS.EnumFlag.其它费用收入单 : Table.PayS.EnumFlag.其它费用支出单);

            FeiYongLeiBie.ItemsSource = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.费用类别, Table.UniqueS.EnumEnable.启用);
            FeiYongLeiBie.SelectedValuePath = "Id";
            FeiYongLeiBie.SelectedValueBinding = new Binding("UniqueSId");
            FeiYongLeiBie.DisplayMemberPath = "Name";

            uCUniqueSBank.Init(Table.UniqueS.EnumFlag.支付方式);
            label1.Content = this.Title = tableText.ToString();
            dataGridObject.SelectedValuePath = "Money";
            InitOrder();
        }

        private void InitOrder()
        {
            dateTimeUpDownKaiDan.Value = order.OrderDateTime;
            label1OrderNo.Content = order.OrderNO;
            textBoxNote.Text = order.Note;
            dataGridObject.ItemsSource = order._PayDetail;
            uCUniqueSBank.SelectedObjectId = order.BankId;

            if (order.Enable == Table.PayS.EnumEnable.审核)
            {
                buttonOK.IsEnabled = false;
                dataGridObject.IsReadOnly = true;
            }
            }

        private void InitEvent()
        {
            //dataGridObject.PreparingCellForEdit+=(ss,ee)=>{PreparingCellEdit(ee);};
            dataGridObject.CellEditEnding += (ss, ee) => { CheckCellEditValue(ee); };
            buttonOK.Click += (ss, ee) => { Save(); };
        }

        //double tempValue=0;
        //private void PreparingCellEdit(DataGridPreparingCellForEditEventArgs ee)
        //{
        //    DataGridColumn textColumn = ee.Column as DataGridColumn;
        //    switch (textColumn.Header.ToString())
        //    {
        //        case "金额":
        //            tempValue = 0;
        //            MessageBox.Show(dataGridObject.SelectedValue.ToString());
        //            //double.TryParse(dataGridObject.sele, out tempValue);
        //            break;
        //    }
        //}
        private void CheckCellEditValue(DataGridCellEditEndingEventArgs ee)
        {
            DataGridColumn textColumn = ee.Column as DataGridColumn;
            double d = 0;
            switch (textColumn.Header.ToString())
            {
                case "金额":
                    TextBox textBox = (TextBox)ee.EditingElement;
                    if (!double.TryParse(textBox.Text, out d))
                        textBox.Undo();
                    else
                        if (d < 0)
                            textBox.Undo();

                    //if (d != 0)
                    //    doubleUpDownDoubleValue.Value += d - tempValue;
                    break;
            }
        }

        private void Save()
        {
            order.OrderDateTime = dateTimeUpDownKaiDan.Value;
            order.Note = textBoxNote.Text;
            order.BankId = uCUniqueSBank.SelectedObjectId;
            foreach (Table.PayS.PayDetail p in order._PayDetail)
                order.Money += p.Money;

            order.RealMoney = order.Money;

            Table.DataTableS.EnumDatabaseStatus result = order.Insert();
            if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
                this.Close();
            else
                MessageWindow.Show(result.ToString());
        }

        //private void SetMoney()
        //{
        //    if (dataGridObject.SelectedItem == null)
        //        return;

        //    Table.PayS ps = (Table.PayS)dataGridObject.SelectedItem;
        //    var cntr = dataGridObject.ItemContainerGenerator.ContainerFromItem(dataGridObject.SelectedItem);
        //    DataGridRow ObjROw = (DataGridRow)cntr;
        //    if (ObjROw != null)
        //    {
        //        FrameworkElement objElement = dataGridObject.Columns[0].GetCellContent(ObjROw);
        //        if (objElement != null)
        //        {
        //            System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement;
        //            if (objChk.IsChecked != true)
        //                selectMoney += ps.Money;
        //            else
        //                selectMoney -= ps.Money;
        //        }
        //    }
        //    labelMoney.Content = selectMoney;
        //}
    }
}
